package com.struts2.model;

import java.util.List;





import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;

import com.hibernate.HibernateUtil;

public class CartItem {

	private int productItemId;
	private int quantity;
	
	public int getProductItemId() {
		return productItemId;
	}

	public void setProductItemId(int productItemId) {
		this.productItemId = productItemId;
	}

	public int getQuantity() {
		return quantity;
	}

	public void setQuantity(int quantity) {
		this.quantity = quantity;
	}
	
	public int getCartItemProductLineId() {
		
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		
		String hql = "SELECT I FROM Product_item I"
				+ " LEFT JOIN fetch I.product_line"
				+ " WHERE I.id = :productItemId";
		
		Query query = session.createQuery(hql);
		query.setInteger("productItemId", productItemId);
		
		@SuppressWarnings("unchecked")
		List<Product_item> results = (List<Product_item>) query.list();
		int productLineId = results.get(0).getProduct_line().getId();
		
		return productLineId;
	}

	public int getCartItemQuantity() {
		
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		
		Criteria criteria = session.createCriteria(Product_item.class);
	    criteria.add(Restrictions.eq("id", productItemId));
		
		@SuppressWarnings("unchecked")
		List<Product_item> product_itemList = (List<Product_item>) criteria.list();
		int quantity = product_itemList.get(0).getQuantity();
						
		return quantity;
	}
		
	public String getCartItemName() {
		
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		
		String hql = "SELECT I FROM Product_item I"
				+ " LEFT JOIN fetch I.product_line"
				+ " WHERE I.id = :productItemId";
		
		Query query = session.createQuery(hql);
		query.setInteger("productItemId", productItemId);
		
		@SuppressWarnings("unchecked")
		List<Product_item> results = (List<Product_item>) query.list();
		String productName = results.get(0).getProduct_line().getName();

		return productName;
	}
	
	public String getCartItemColor() {
		
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		
		String hql = "SELECT I FROM Product_item I"
				+ " LEFT JOIN fetch I.color"
				+ " WHERE I.id = :productItemId";
		
		Query query = session.createQuery(hql);
		query.setInteger("productItemId", productItemId);
		
		@SuppressWarnings("unchecked")
		List<Product_item> results = (List<Product_item>) query.list();
		String productColor = results.get(0).getColor().getName();

		return productColor;
	}
	
	public String getCartItemSize() {
		
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		
		String hql = "SELECT I FROM Product_item I"
				+ " LEFT JOIN fetch I.size"
				+ " WHERE I.id = :productItemId";
		
		Query query = session.createQuery(hql);
		query.setInteger("productItemId", productItemId);
		
		@SuppressWarnings("unchecked")
		List<Product_item> results = (List<Product_item>) query.list();
		String productSize = results.get(0).getSize().getName();

		return productSize;
	}
	
	public int getCartItemPrice() {
		
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		
		String hql = "SELECT I FROM Product_item I"
				+ " LEFT JOIN fetch I.product_line"
				+ " WHERE I.id = :productItemId";
		
		Query query = session.createQuery(hql);
		query.setInteger("productItemId", productItemId);
		
		@SuppressWarnings("unchecked")
		List<Product_item> results = (List<Product_item>) query.list();
		int productPrice = results.get(0).getProduct_line().getPrice();

		return productPrice;
	}
	
	public int getCartItemAmount() {
		
		return quantity*getCartItemPrice();
	}
	
	public void deductCartItemsQuantity() {
	}

}